iT邦幫忙

2024 iThome 鐵人賽

DAY 27
0
Software Development

開發都來不及了還做什麼測試系列 第 27

Day27. 用Playwright來進行自動化測試 - 5

  • 分享至 

  • xImage
  •  

利用Playwright來下載檔案

https://playwright.dev/docs/downloads

https://ithelp.ithome.com.tw/upload/images/20240914/20108631Kwudx8iAx2.png

const { test, expect } = require('@playwright/test');

test.describe("Example Tests", () => {

    test("Examples", async ({page}) => {
	    await page.goto("https://the-internet.herokuapp.com/download");
	    const [download] = await Promise.all([
		    page.waitForEvent('download'),
		    page.locator('text=some-file.txt').click(),
	    ]);
	    const path = await download.path();
	    const url = download.url();
	    console.log(path);
	    console.log(url);
    })
})

利用playwright來上傳檔案

https://the-internet.herokuapp.com/upload

https://ithelp.ithome.com.tw/upload/images/20240914/201086317boBgpzrbq.png

const { test, expect } = require('@playwright/test');

test.describe("Example Tests", () => {

    test("Examples", async ({page}) => {
	    await page.goto("https://the-internet.herokuapp.com/upload");

		const [fileChooser] = await Promise.all([
			page.waitForEvent('filechooser'),
			page.locator('#file-upload').click(),
		])
	    await fileChooser.setFiles('uploadedFiles/sample.pdf');
	    await page.locator('input:has-text("Upload")').click();
	    await expect(page.locator('text=File Uploaded!')).toBeVisible();
	    await expect(page.locator('text=sample.pdf')).toBeVisible();
    })
})

利用playwright來產生PDF檔

https://ithelp.ithome.com.tw/upload/images/20240914/2010863179DUXqJDBx.png

npx playwright pdf --viewport-size="1280,720" https://parabank.parasoft.com/parabank/services.htm sample.pdf

Hovering Over 滑過顯示的測試

https://the-internet.herokuapp.com/hovers

https://ithelp.ithome.com.tw/upload/images/20240914/20108631vWZobkIuAA.png

const { test, expect } = require('@playwright/test');

test.describe("Example Tests", () => {

    test("Examples", async ({page}) => {
	    await page.goto("https://the-internet.herokuapp.com/hovers");

		await page.hover('[alt="User Avatar"]');
		await expect(page.locator('text=name: user1')).toBeVisible();
		await page.locator('text=View profile').first().click()
		await page.pause()
    })
})

Auto-waiting 自動等待

https://playwright.dev/docs/actionability

Playwright 在執行操作之前會對element執行一系列可操作性檢查,以確保操作按預期的方式工作。
它會自動等待所有相關檢查通過,再執行請求的操作。如果在給timeout時間內未通過所需的檢查,則操作會顯示失敗並丟出 TimeoutError。

Dialogs

Playwright 可以與 Web 對話方塊互相作用,例如alert, confirm, prompt 和beforeunload。

https://playwright.dev/docs/dialogs

利用playwright安裝瀏覽器

https://ithelp.ithome.com.tw/upload/images/20240914/20108631OkLF8TqR2O.png

npx playwright install webkit
npx playwright install msedge

NPX Playwright 一些設定

https://ithelp.ithome.com.tw/upload/images/20240914/201086313ruUhC5WAF.png

利用playwright來打開瀏覽器

# Open website with webkit
npx playwright wk https://parabank.parasoft.com
# Open website with chromium
npx playwright cr https://parabank.parasoft.com

Key press and shortcut 鍵盤事件

https://ithelp.ithome.com.tw/upload/images/20240914/201086316ee9hVsvuz.png

https://the-internet.herokuapp.com/key_presses

const { test, expect } = require('@playwright/test');

test.describe("Example Tests", () => {

    test("Examples", async ({page}) => {
	    await page.goto("https://the-internet.herokuapp.com/key_presses");

		await page.press('#target', 'F1')
		await page.press('#target', 'Delete')
    })
})

利用Playwright CLI來做螢幕截圖

https://ithelp.ithome.com.tw/upload/images/20240914/20108631aM8JsD9Pdr.png

npx playwright screenshot -b webkit https://wikipedia.com wikipedia.png
npx playwright screenshot --full-page -b webkit https://wikipedia.com wikipedia.png
npx playwright screenshot --full-page --device="iPhone 11" -b webkit https://wikipedia.com wikipedia.png

https://ithelp.ithome.com.tw/upload/images/20240914/20108631fJl1pYEO7T.png

本文章同步發布於個人blogger


上一篇
Day26. 用Playwright來進行自動化測試 - 4
下一篇
Day28. 白箱測試 - 1
系列文
開發都來不及了還做什麼測試30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言